<?php
/**
 * Created by PhpStorm.
 * User: lzy
 * Date: 2018/11/10
 * Time: 9:42
 */
namespace App\FreeBird\Controllers\Admin;

use App\Admin\Contracts\MenuInterface;
use App\Base\Contracts\AreaInterface;
use App\Base\Controllers\Controller;
use App\FreeBird\Contracts\InfoClassInterface;
use App\FreeBird\Contracts\StoreInterface;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class StoreController extends Controller
{



    private  $formatTree;
    /**
     * 门点接口
     * @return StoreInterface
     */
    public function getStoreInterface()
    {
        return app()->make(StoreInterface::class);
    }
    /**
     * 菜单接口
     * @return MenuInterface
     */
    public function getMenuInterface(){
        return app()->make(MenuInterface::class);
    }

    /**
     * 请选择省区
     * @return AreaInterface
     */
    public function getAreaInterface(){
        return app()->make(AreaInterface::class);
    }

    /**
     * 获取分类
     * @return mixed
     */
    public function getInfoClassInterface(){
        return app()->make(InfoClassInterface::class);
    }

    /**
     * 首页
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {

        if ($request->getMethod() == 'POST') {
            $where = function ($query) use ($request) {
                $stroe_name = $request->input("stroe_name");
                if (!empty($stroe_name)) {
                    $query->where("stroe_name", "like", '%' . $stroe_name . '%');
                }
                $tel = $request->input("tel");
                if (!empty($tel)) {
                    $query->where("tel", "like", '%' . $tel . '%');
                }
                $class = $request->input("class");
                if (!empty($class)) {
                    $query->where("class", $class);
                }
            };
            return $this->getStoreInterface()->getList($where);
        }
        $menuList=$this->getInfoClassInterface()->getClassAll([]);
        $tree = list_to_tree($menuList->toArray());
        $this->formatTree = array();
        $this->_toFormatTree($tree,0,"name");
        return view("freebird.store.index")->with("tree", $this->formatTree);
    }

    /**
     * 编辑
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */

    public function edit(Request $request)
    {
        $id = $request->input("id");
        if ($request->getMethod() == 'POST') {
            $validator = Validator::make($request->all(), [
                'stroe_name' => 'required',
                'remark' => 'required',
            ], ["title.required" => '标题必须有值', "content.required" => '标题必须有值']);
            if ($validator->fails()) {
                $this->throwValidatorException($validator);
            }
            return $this->getStoreInterface()->saveData($request->all());
        }
        $menuList=$this->getInfoClassInterface()->getClassAll([]);
        $tree = list_to_tree($menuList->toArray());
        $this->formatTree = array();
        $this->_toFormatTree($tree,0,"name");

        $areaList=$this->getAreaInterface()->getAreaList(["pid"=>0]);
        $data = $this->getStoreInterface()->getInfo(["id"=>$id]);
        return view("freebird.store.edit")->with("data", $data)->with("areaList",$areaList)->with("tree", $this->formatTree);
    }

    /**
     * 添加
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function add(Request $request)
    {

        if ($request->getMethod() == 'POST') {
            $validator = Validator::make($request->all(), [
                'stroe_name' => 'required',
                'remark' => 'required',
            ], ["stroe_name.required" => '商家名称', "remark.required" => '备注']);
            if ($validator->fails()) {
                $this->throwValidatorException($validator);
            }
            return $this->getStoreInterface()->saveData($request->all());
        }
        //获取分类

        $menuList=$this->getInfoClassInterface()->getClassAll([]);

        $tree = list_to_tree($menuList->toArray());
        $this->formatTree = array();
        $this->_toFormatTree($tree,0,"name");

       $areaList=$this->getAreaInterface()->getAreaList(["pid"=>0]);

        return view("freebird.store.add")->with("areaList",$areaList)->with("tree", $this->formatTree);
    }

    /**
     * 生成树形
     * @author 林子彦 <278805354@qq.com>
     * @param type $list
     * @param type $level
     * @param type $title
     * @return type
     */
    private function _toFormatTree($list, $level = 0, $title = 'title') {

        foreach ($list as $key => $val) {
            $tmp_str = str_repeat("&nbsp;&nbsp;&nbsp;&nbsp;", $level * 2);
            $tmp_str .= "└";

            $val['level'] = $level;
            $val['title_show'] = $level == 0 ? $val[$title] . "&nbsp;" : $tmp_str . $val[$title] . "&nbsp;";
            // $val['title_show'] = $val['id'].'|'.$level.'级|'.$val['title_show'];
            if (!array_key_exists('_child', $val)) {
                array_push($this->formatTree, $val);
            } else {
                $tmp_ary = $val['_child'];
                unset($val['_child']);
                array_push($this->formatTree, $val);
                $this->_toFormatTree($tmp_ary, $level + 1, $title); //进行下一层递归
            }
        }
        return;
    }



    /**
     * 广告删除
     * @param Request $request
     */
    public function del(Request $request)
    {
        if ($request->getMethod() == 'POST') {
            $validator = Validator::make($request->all(), [
                'id' => 'required',
            ], ["id.required" => '删除参数id是必须的']);
            if ($validator->fails()) {
                $this->throwValidatorException($validator);
            }
            $where = function ($query) use ($request) {
                $id = $request->input("id");
                $query->where("id", $id);
            };
            return $this->getStoreInterface()->deleteData($where);
        }
    }

    /**
     * 改变状态
     * @param Request $request
     */
    public function changeStatus(Request $request){
        return  $this->getStoreInterface()->saveData($request->all());
    }


    /**
     * 选择地图
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function map(){
        return view("freebird.store.map");
    }

}